package com.tm;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.tm.utils.ResponseUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Field;

/**
 * @author wangSang
 * @create 2022-09-04 21:40
 */
@Component
public class CZuulFilter extends ZuulFilter {

    @Autowired
    private RedisTemplate redisTemplate;

    //决定过滤器类型
    public String filterType() {
        return "pre";
    }

    //过滤器的顺序
    public int filterOrder() {
        return 0;
    }

    //是否通过过滤
    public boolean shouldFilter() {
        return true;
    }

    //实际上验证的逻辑
    public Object run() throws ZuulException {

        //获取到zuul中的请求上下文对象
        RequestContext currentContext = RequestContext.getCurrentContext();
        //获取到request对象和response对象
        HttpServletRequest request = currentContext.getRequest();
        HttpServletResponse response = currentContext.getResponse();
        //获取请求地址
        String servletPath = request.getServletPath();
        System.out.println(servletPath);

        int i =0;

        if( servletPath.contains("/login/sendSms") || servletPath.contains("/login/logon") || servletPath.contains("/login/quit") ){
            i++;
        }

        if(i==0){
            //判断是否登录
            String token = request.getHeader("token");
            String phoneNumber = request.getHeader("phoneNumber");
            if(StringUtils.isEmpty(token) || StringUtils.isEmpty(phoneNumber) ){

                response.setHeader("tmMassgs","pleaseLogin");
                ResponseUtil.outputJSON(response,200);
                currentContext.setSendZuulResponse(false);

            }
            Object o1 = redisTemplate.opsForValue().get("token_" + phoneNumber);
            if(token.equals(o1.toString())){
                //通过response对象，将成功状态传给前端
                //ResponseUtil.outputJSON(response,200);
                //这里的false代表当前的请求不会往后台的微服务中转发
                currentContext.setSendZuulResponse(true);
                currentContext.setResponseStatusCode(200);

            }else {

                response.setHeader("tmMassgs","pleaseLogon");
                ResponseUtil.outputJSON(response,200);
                currentContext.setSendZuulResponse(false);
                return null;

            }
        }



            //验证请求不能大于 20M  == 20971520B
            Class<HttpServletRequest> httpServletRequestClass = HttpServletRequest.class;

            Field[] declaredFields = httpServletRequestClass.getDeclaredFields();
            Long length = 0l;
            //遍历所有键值对
            for (Field field : declaredFields) {

                //declaredField就是每个值
                String name = field.getName();//字段名
                int length1 = name.length();

                Object o = null;
                try {
                    o = field.get(request);
                } catch (IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
                int length2 = o.toString().length();

                length += (long)length1+length2;
            }

            if(length >= 20971520){

                response.setHeader("tmMassgs","hehehe");
                ResponseUtil.outputJSON(response,200);
                currentContext.setSendZuulResponse(false);
            }else {
                //通过response对象，将成功状态传给前端
                //ResponseUtil.outputJSON(response,200);
                //这里的false代表当前的请求不会往后台的微服务中转发
                currentContext.setSendZuulResponse(true);
                currentContext.setResponseStatusCode(200);


        }


        return null;
    }
}
